package com.spring.format.code.generation.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.spring.format.code.generation.constants.CloumnConstants;
import com.spring.format.code.generation.constants.Constants;
import com.spring.format.code.generation.mybatis.po.TableFieldPO;
import com.spring.format.code.generation.mybatis.vo.JavaAttributeVO;

/**
 * 文件生成帮助类
 * 
 * @author lenovo
 *
 */
public class FieldUtil {
	private FieldUtil() {

	}

	/**
	 * 不可编辑字段
	 */
	protected static final List<String> notUpdateFieldList = new ArrayList<>();
	/**
	 * 排序字段
	 */
	protected static final List<String> orderByFieldList = new ArrayList<>();

	/**
	 * 有自己update方法的地段
	 */
	protected static final List<String> haveSelfUpdateFieldList = new ArrayList<>();
	/**
	 * 有自己query方法的地段
	 */
	protected static final List<String> haveSelfQueryFieldList = new ArrayList<>();

	public static List<String> getNotupdatefieldlist() {
		return notUpdateFieldList;
	}

	public static List<String> getOrderbyfieldlist() {
		return orderByFieldList;
	}

	public static List<String> getHaveselfupdatefieldlist() {
		return haveSelfUpdateFieldList;
	}

	public static List<String> getHaveselfqueryfieldlist() {
		return haveSelfQueryFieldList;
	}

	/**
	 * 默认加载不需要update字段
	 */
	static {
		// 不可编辑阻断
		String fieldStr = PropertiesUtil.getValue(Constants.NOT_UPDATE_FIELD);
		if (StringUtils.isNotBlank(fieldStr)) {
			String[] fields = fieldStr.split(",");
			for (String field : fields) {
				notUpdateFieldList.add(field);
			}
		}
		// 排序字段
		String orderByField = PropertiesUtil.getValue(Constants.ORDER_BY_FIELD);
		if (StringUtils.isNotBlank(orderByField)) {
			String[] fields = orderByField.split(",");
			for (String field : fields) {
				orderByFieldList.add(field);
			}
		}
		// 有自己update方法的字段
		String haveSelfUpdate = PropertiesUtil.getValue(Constants.HAVE_SELF_UPDATE_MEHTOD);
		if (StringUtils.isNotBlank(haveSelfUpdate)) {
			String[] fields = haveSelfUpdate.split(",");
			for (String field : fields) {
				haveSelfUpdateFieldList.add(field);
			}
		}
		// 有自己query方法的字段
		String haveSelfQuery = PropertiesUtil.getValue(Constants.HAVE_SELF_QUERY_MEHTOD);
		if (StringUtils.isNotBlank(haveSelfQuery)) {
			String[] fields = haveSelfQuery.split(",");
			for (String field : fields) {
				haveSelfQueryFieldList.add(field);
			}
		}
	}

	public static String fieldType(TableFieldPO filedPO) {
		String returnType = "String";
		switch (filedPO.getData_type()) {
		case CloumnConstants.MYSQL_INT:
			if (null != filedPO.getNumeric_precision()) {
				if (filedPO.getNumeric_precision().intValue() <= 10) {
					returnType = CloumnConstants.JAVA_INTEGER;
				} else if (filedPO.getNumeric_precision().intValue() > 10
						&& filedPO.getNumeric_precision().intValue() <= 19) {
					returnType = CloumnConstants.JAVA_LONG;
				} else if (filedPO.getNumeric_precision().intValue() > 19) {
					returnType = CloumnConstants.JAVA_BIG_INTEGER;
				}
			} else {
				returnType = CloumnConstants.JAVA_INTEGER;
			}
			break;
		case CloumnConstants.MYSQL_BIGINT:
			returnType = CloumnConstants.JAVA_BIG_INTEGER;
			break;
		case CloumnConstants.MYSQL_TIMES_TAMP:
			returnType = CloumnConstants.JAVA_TIME_STAMP;
			break;
		case CloumnConstants.MYSQL_VARCHAR:
			returnType = CloumnConstants.JAVA_STRING;
			break;
		case CloumnConstants.MYSQL_DATE_TIME:
			returnType = CloumnConstants.JAVA_DATE;
			break;
		case CloumnConstants.MYSQL_DATE:
			returnType = CloumnConstants.JAVA_DATE;
			break;
		case CloumnConstants.MYSQL_CHAR:
			returnType = CloumnConstants.JAVA_STRING;
			break;
		case CloumnConstants.MYSQL_NUMERIC:
			returnType = CloumnConstants.JAVA_BIG_DECIMAL;
			break;
		case CloumnConstants.MYSQL_DECIMAL:
			returnType = CloumnConstants.JAVA_BIG_DECIMAL;
			break;
		case CloumnConstants.MYSQL_DOUBLE:
			returnType = CloumnConstants.JAVA_BIG_DECIMAL;
			break;
		default:
			break;
		}
		return returnType;
	}

	/** 拼接 get方法 **/
	public static String getMethod(String columnName) {
		return "get" + NameUtil.firstCharacterToUpper(columnName);
	}

	/** 拼接 set方法 **/
	public static String setMethod(String columnName) {
		return "set" + NameUtil.firstCharacterToUpper(columnName);
	}

	// /*** 枚举字段处理 ***/
	// public static String[] isEnum(TableFieldPO filedPO, Boolean attribute_hump,
	// String prefix, String getMethod) {
	//
	// // 不是枚举字段直接返回null
	// if (!filedPO.isNeedImport()) {
	// return null;
	// }
	// StringBuilder methodStr = new StringBuilder();
	// StringBuilder attributeStr = new StringBuilder();
	//
	// String fieldName = filedPO.getColumn_name();
	// // 驼峰
	// if (attribute_hump) {
	// fieldName += "Str";
	// } else {// 不是驼峰
	// fieldName += "_str";
	// }
	// // 注释，属性
	// attributeStr.append(" /** ").append(filedPO.getColumn_comment()).append("字符串
	// **/ \n ");
	// attributeStr.append(" private String ").append(fieldName).append(";\n");
	//
	// // get，set方法
	// methodStr.append(" public String
	// ").append(FieldUtil.getMethod(fieldName.toLowerCase())).append("() {\n")
	// .append(" if(StringUtils.isBlank(").append(fieldName).append(")&& null !=
	// ").append(prefix)
	// .append(getMethod).append("()){\n").append(" ").append(fieldName).append("=")
	// .append(prefix).append(getMethod).append("().getDescription();\n").append("
	// }\n")
	// .append(" return this.").append(fieldName).append(";\n").append("
	// }\n").append("\n");
	// methodStr.append(" public void
	// ").append(FieldUtil.setMethod(fieldName)).append("(String
	// ").append(fieldName)
	// .append(") {\n").append(" this.").append(fieldName).append(" =
	// ").append(fieldName).append(";\n")
	// .append(" }\n").append("\n");
	//
	// String[] strArray = new String[2];
	// strArray[0] = attributeStr.toString();
	// strArray[1] = methodStr.toString();
	// return strArray;
	// }

	// 需要追加字符串的字段
	public static String[] voNeedStr(TableFieldPO filedPO, Boolean attribute_hump, String prefix, String getMethod) {
		// 不需要引入的直接返回null
		if (!ImportClassUtil.voNeedStr(filedPO.getData_type())) {
			return null;
		}
		StringBuilder methodStr = new StringBuilder();
		StringBuilder attributeStr = new StringBuilder();

		String fieldName = filedPO.getColumn_name();
		// 需要添加字符串的字段，比如Date类型 需要字符串显示

		// 驼峰
		if (attribute_hump) {
			fieldName += "Str";
		} else {// 不是驼峰
			fieldName += "_str";
		}
		// 注释，属性
		attributeStr.append("	/** ").append(filedPO.getColumn_comment()).append("字符串 **/ \n ");
		attributeStr.append("	private String  ").append(fieldName).append(";\n");
		// get，set方法
		methodStr.append("	public String ").append(FieldUtil.getMethod(fieldName.toLowerCase())).append("() {\n")
				.append(" 		if(StringUtils.isBlank(").append(fieldName).append(")&& null != ").append(prefix)
				.append(getMethod).append("()){\n").append("        			").append(fieldName).append("=")
				.append(ImportClassUtil.voNeedStrMethodInsert(filedPO.getData_type(), prefix, getMethod)).append(";\n")
				.append("       }\n").append("		return this.").append(fieldName).append(";\n").append("	}\n")
				.append("\n");
		methodStr.append("	public void ").append(FieldUtil.setMethod(fieldName)).append("(String ").append(fieldName)
				.append(") {\n").append("		this.").append(fieldName).append(" = ").append(fieldName).append(";\n")
				.append("	}\n").append("\n");
		String[] strArray = new String[2];
		strArray[0] = attributeStr.toString();
		strArray[1] = methodStr.toString();
		return strArray;

	}

	public static String[] importFieldVO(JavaAttributeVO attrVO) {
		StringBuilder methodStr = new StringBuilder();
		StringBuilder attributeStr = new StringBuilder();
		if (CloumnConstants.JAVA_STRING.equals(attrVO.getType())) {
			attributeStr.append("@JsonInclude(JsonInclude.Include.NON_EMPTY) \n ");
		} else {
			attributeStr.append("@JsonInclude(JsonInclude.Include.NON_NULL) \n ");
		}
		attributeStr.append("    @ApiModelProperty(value = \"").append(attrVO.getColumnComment()).append("\",name=\"")
				.append(attrVO.getName()).append("\") \n");
		// po内没有必要放验证，放在form里最好
		attributeStr.append("	private ").append(attrVO.getType()).append("  ").append(attrVO.getName()).append(";\n");
		// get，set方法
		methodStr.append("	public ").append(attrVO.getType()).append(" ").append(attrVO.getMethod()).append("() {\n");
		methodStr.append("		return this.").append(attrVO.getName()).append(";\n");
		methodStr.append("	}\n").append("\n");
		methodStr.append("	public void ").append(attrVO.setMethod()).append("(").append(attrVO.getType()).append(" ")
				.append(attrVO.getName()).append(") {\n").append("		this.").append(attrVO.getName()).append(" = ")
				.append(attrVO.getName()).append(";\n").append("	}\n").append("\n");

		String[] importArray = new String[2];
		importArray[0] = attributeStr.toString();
		importArray[1] = methodStr.toString();
		return importArray;
	}

	/**
	 * po 使用
	 * 
	 * @param attrVO
	 * @return
	 */
	public static String[] importField(JavaAttributeVO attrVO) {
		StringBuilder methodStr = new StringBuilder();
		StringBuilder attributeStr = new StringBuilder();
		attributeStr.append("	/** ").append(attrVO.getColumnComment()).append(" **/ \n ");
		// po内没有必要放验证，放在form里最好
		if (CloumnConstants.JAVA_STRING.equals(attrVO.getType())) {
			attributeStr.append("@JsonInclude(JsonInclude.Include.NON_EMPTY) \n ");
		} else {
			attributeStr.append("@JsonInclude(JsonInclude.Include.NON_NULL) \n ");
		}
		attributeStr.append("");
		attributeStr.append("	private ").append(attrVO.getType()).append("  ").append(attrVO.getName()).append(";\n");
		// get，set方法
		methodStr.append("	public ").append(attrVO.getType()).append(" ").append(attrVO.getMethod()).append("() {\n");
		methodStr.append("		return this.").append(attrVO.getName()).append(";\n");
		methodStr.append("	}\n").append("\n");
		methodStr.append("	public void ").append(attrVO.setMethod()).append("(").append(attrVO.getType()).append(" ")
				.append(attrVO.getName()).append(") {\n").append("		this.").append(attrVO.getName()).append(" = ")
				.append(attrVO.getName()).append(";\n").append("	}\n").append("\n");

		String[] importArray = new String[2];
		importArray[0] = attributeStr.toString();
		importArray[1] = methodStr.toString();
		return importArray;
	}

}
